#!/bin/bash
#
#   Borrowed with love from BunsenLabs Conky Editor
#
#   Written by damo <damo@bunsenlabs.org> for BunsenLabs Linux, April 2015
#
########################################################################
#
#   Conkys must be in $CONKYPATH
#   The name must end with "conky" or "conkyrc"
#
#   Checkmarked conkys will be opened in the text editor
#   Multiple conkys can be chosen
#
########################################################################

CONKYPATH="$HOME/.config/conky"

### DIALOG VARIABLES
DLGDEC="yad --center  --borders=20 --width=400 --height=500 "
TITLE="ArcoLinux Conky Edit"
WINICON="--window-icon=distributor-logo-arcolinux"
OK="--button=yad-ok:0"
CANCEL="--button=yad-cancel:1"

########## FUNCTIONS ###################################################

fillArrays(){
    num="$1"
    conkysPath[$num]="$2"   # full filepath to conky
    conkysArr[$num]="$3"    # displayed name: "directory/*conky(rc)"
}

findConky(){
# search dirs for conkys files - looking for "conky" in the name
# if "*conky(rc)" then display it

num=0
# find files in CONKYPATH with conky in the name
for x in $(find "$CONKYPATH" -type f );do
    f=$(basename "$x")    # filename from filepath
    if [[ $f = *conkyrc ]] || [[ $f = *conky ]];then
        # filename ends with *conky or *conkyrc
        # get directory/conkyname to display in list
        CONKY=$( echo "$x" | awk -F"/" '{print $(NF-1)"/"$NF}')
        fillArrays $num "$x" "$CONKY"
        num=$(($num+1))
    fi
done
}
######## END FUNCTIONS #################################################

# get conky directories in .conky, add to array
findConky

# loop through arrays, and build msg text for yad dialog
unset LISTCONKY
for ((j=0; j<${#conkysArr[*]}; j++));do
    LISTCONKY="$LISTCONKY FALSE ${conkysArr[j]}"
done

## Populate yad dialog from array, get return value(s)
RET=$($DLGDEC $WINICON --list --title="$TITLE" \
    --text="Select Conkys to edit from the list\nMultiple Conkys can be chosen" \
    --checklist \
    --column="Select:CHK" --column="Conky Name:TXT" $LISTCONKY --separator=":"\
    $OK $CANCEL \
    )

if [[ $? == 1 ]]; then # cancel button pressed
    exit 0
else
    i=0
    OIFS=$IFS   # save Internal Field Separator
    IFS=":"     # separator is ":" in returned choices
    for name in $RET; do
        retConky[$i]="$name"
        i=$(($i+1))
    done
    IFS=$OIFS   # reset IFS back to default

    # Find the chosen conkys and edit them
    for name in ${retConky[*]};do       # loop through checkmarked conky names
        for ((j=0; j<${#conkysPath[*]}; j++));do  # traverse through elements
            for f in ${conkysPath[j]};do    # compare with choice from dialog
                display=$( echo "$f" | awk -F"/" '{print $(NF-1)"/"$NF}')
                if [[ $display = $name ]];then
                    geany "$f"
                fi
            done
        done
    done
fi

exit 0
